﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Default : System.Web.UI.Page
{
    TiemPhongDataContext db = new TiemPhongDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataListView();
        }
    }
    protected void btnThem_Click1(object sender, EventArgs e)
    {
        try
        {
            // khởi tạo đối tượng Vacxin mới vx
            Vac_xin vx = new Vac_xin();
            // gán các giá trị cần thêm vào
            vx.MaVC = txtMVX.Text;
            vx.TenVC = txtTVX.Text;
            vx.SoLuong = Convert.ToInt32(txtSL.Text);
            vx.DonGia = Convert.ToDecimal(txtDG.Text);
            vx.GhiChu = txtGC.Text;
            db.Vac_xins.InsertOnSubmit(vx);
            db.SubmitChanges();
            // load lại listView
            loadDataListView();
            ClearControl();
            // hiện thông báo
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Thêm mới thành công !";
        }
        catch (Exception m)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Lỗi thêm mới: " + m.Message;
        }
    }
    protected void btnSua_Click1(object sender, EventArgs e)
    {
        try
        {

            var result = db.Vac_xins.Where(v => v.MaVC == txtMVX.Text).SingleOrDefault();
            if (result != null)
            {
                result.TenVC = txtTVX.Text;
                result.SoLuong = Convert.ToInt32(txtSL.Text);
                result.DonGia = Convert.ToInt32(txtDG.Text);
                result.GhiChu = txtGC.Text;
                db.SubmitChanges();
            }
            // load lại listView
            loadDataListView();
            ClearControl();
            // hiện thông báo
            messageKetQua.Enabled = true;
            btnThem.Enabled = true;
            btnSua.Enabled = false;
            messageKetQua.Text = "Sửa thông tin thành công !";
        }
        catch (Exception m)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Sửa thông tin lỗi: " + m.Message;
        }
    }

    protected void loadDataListView()
    {
        var list = db.Vac_xins.ToList();
        this.ListViewVacxin.DataSource = list;
        this.ListViewVacxin.DataBind();
        txtMVX.Enabled = false;
        txtMVX.Text = loadMaVC();
    }
    // hàm tính MaVC
    protected string loadMaVC()
    {
        string countID = "";
        var selectID = (from u in db.Vac_xins select u.MaVC).Count() + 1;
        countID = selectID.ToString();
        countID = "VC" + countID;
        return countID;
    }    

    protected void ClearControl()
    {
        txtTVX.Text = String.Empty;
        txtSL.Text = String.Empty;
        txtDG.Text = String.Empty;
        txtGC.Text = String.Empty;

    }

    protected void ListViewVacxin_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        var idMVC = e.CommandArgument.ToString();
        var ResultVacXin = db.Vac_xins.FirstOrDefault(v => v.MaVC.Equals(idMVC));
        if (e.CommandName == "Delete")
        {
            var deleteResultCTVacXin = from c in db.ChiTietVac_xins
                                       where c.MaVC == ResultVacXin.MaVC
                                       select c; ;
            if (deleteResultCTVacXin != null)
            {
                db.ChiTietVac_xins.DeleteAllOnSubmit(deleteResultCTVacXin);
            }
            if (ResultVacXin != null)
                db.Vac_xins.DeleteOnSubmit(ResultVacXin);
            db.SubmitChanges();
            ClearControl();
            loadDataListView();
        }
        if (e.CommandName == "Update")
        {
            txtMVX.Text = ResultVacXin.MaVC;
            txtTVX.Text = ResultVacXin.TenVC;
            txtSL.Text = ResultVacXin.SoLuong.ToString();
            txtDG.Text = ResultVacXin.DonGia.ToString();
            txtGC.Text = ResultVacXin.GhiChu;
            btnSua.Enabled = true;
            btnThem.Enabled = false;
        }
    }
    protected void ListViewVacxin_OnItemDeleting(object sender, ListViewDeleteEventArgs e){}
    protected void ListViewVacxin_OnItemUpdating(object sender, ListViewUpdateEventArgs e){}
}